/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import domain.Hotelroom;
import domain.OrderRoom;
import domain.Orders;
import domain.Passport;
import domain.Users;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.Interval;
import org.joda.time.convert.IntervalConverter;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultScheduleEvent;
import org.primefaces.model.DefaultScheduleModel;
import org.primefaces.model.ScheduleEvent;
import org.primefaces.model.ScheduleModel;
import services.impl.HotelRoomServices;
import services.impl.OrderRoomServices;
import services.impl.OrdersServices;
import services.impl.PassportServices;
import services.impl.UserServiceImpl;

/**
 *
 * @author amega
 */
@ManagedBean(name="orderBean")
@SessionScoped
public class OrderBean {
    
    private Hotelroom hotelRoomOrder;
    
    private ScheduleModel ordersRoomDate = new DefaultScheduleModel();
    
    private ScheduleEvent event = new DefaultScheduleEvent(); 
    
    @EJB
    private OrderRoomServices orderRoomServices;
    
    @EJB 
    private UserServiceImpl userServices;
    
    @EJB
    private HotelRoomServices hotelRoomServices;
    
    @EJB
    private OrdersServices orderServices;
    
    private Orders selectedOrders;

    public Orders getSelectedOrders() {
        return selectedOrders;
    }

    public void setSelectedOrders(Orders selectedOrders) {
        this.selectedOrders = selectedOrders;
    }
    
    

    public ScheduleModel getOrdersRoomDate() {
        getOrderRoomDate();
        return ordersRoomDate;
    }

    public void setOrdersRoomDate(ScheduleModel ordersRoomDate) {
        this.ordersRoomDate = ordersRoomDate;
    }
    
    
    
    public Hotelroom getHotelRoomOrder() {
        return hotelRoomOrder;
    }

    public void setHotelRoomOrder(Hotelroom hotelRoomOrder) {
        
        this.hotelRoomOrder = hotelRoomOrder;
    }
    
    public void getOrderRoomDate () {
        Users u = userServices.find(Integer.parseInt("3")); 
        List<OrderRoom> or = orderRoomServices.getRoomOrdersDate(hotelRoomOrder, u);
        ordersRoomDate.clear();
        for (OrderRoom o: or) {
            ordersRoomDate.addEvent(new DefaultScheduleEvent(o.getStatus(),o.getDateBegin(),o.getDateEnd()));
            
            System.out.println(o.getDateBegin());
        }
    }
    
    public void orderPaid() {
        selectedOrders.setIsPaid(true);
        orderServices.edit(selectedOrders);
    }
    
    public void test() {
        
        Users u = userServices.find(Integer.parseInt("3")); 
        
        System.out.println(u.getEmail()+" \\n");
        Hotelroom hr = hotelRoomServices.find(Integer.parseInt("7"));
        System.out.println(hr.getNumberRoom()+" \\n");
        
        List<OrderRoom> or ;
        or  = orderRoomServices.getRoomOrdersDate(hr, u);
        for (OrderRoom o: or) {
            System.out.println(o.getDateBegin());
        }
        
    }
    
    public void onEventSelect( SelectEvent selectEvent) {  
        System.out.println("Event Selected");
        //System.out.println(selectEvent.getComponent().);
        event = (ScheduleEvent) selectEvent.getObject();  
    }  

    public ScheduleEvent getEvent() {
        return event;
    }

    public void setEvent(ScheduleEvent event) {
        this.event = event;
    }
      
    public void addEvent(ActionEvent actionEvent) {  
        if(event.getId() == null)  
            ordersRoomDate.addEvent(event);  
        else  
            ordersRoomDate.updateEvent(event);  
          
        event = new DefaultScheduleEvent();  
    }
    
    public List<Orders> getAllOrders() {
        return orderServices.findAll();
    }
    
    private String userOrderEmail;

    @EJB
    private UserServiceImpl userServiceImpl;
    
    private Users userOrder = new Users();

    public Users getUserOrder() {
        return userOrder;
    }

    public void setUserOrder(Users userOrder) {
        this.userOrder = userOrder;
    }
    
    
    
    public String getUserOrderEmail() {
        return userOrderEmail;
    }

    public void setUserOrderEmail(String userOrderEmail) {
        this.userOrderEmail = userOrderEmail;
    }
    
    public void findUserByEmail() {
        Users u1 = userServiceImpl.getUserByEmail(userOrderEmail);
        if ( u1!= null ) {
            this.userOrder = u1;
        } else {
            this.userOrder = new Users();
        }
        
    }
    
    private String userPassportNumber;

    private Passport userOrderPassport;
    
    @EJB
    private PassportServices passportServices; 

    public Passport getUserOrderPassport() {
        return userOrderPassport;
    }

    public void setUserOrderPassport(Passport userOrderPassport) {
        this.userOrderPassport = userOrderPassport;
    }
    
    
    
    public String getUserPassportNumber() {
        return userPassportNumber;
    }

    public void setUserPassportNumber(String userPassportNumber) {
        this.userPassportNumber = userPassportNumber;
    }
    
    public void findUserPassportByNumber(){
        Passport p = passportServices.getPassportByNumber(userPassportNumber);
        if (p != null) {
            userOrderPassport = p;
        } else {
            userOrderPassport = new Passport(); 
        }
    }
    
    private OrderRoom orderRoom = new OrderRoom();

    public OrderRoom getOrderRoom() {
        return orderRoom;
    }

    public void setOrderRoom(OrderRoom orderRoom) {
        this.orderRoom = orderRoom;
    }
    
    public double prepay;

    public double getPrepay() {
        return prepay;
    }

    public void setPrepay(double prepay) {
        this.prepay = prepay;
    }
    
    
    
    public String getOrderPaid() {
        
        DateTime dt1 = new DateTime(orderRoom.getDateEnd());
        DateTime dt2 = new DateTime(orderRoom.getDateBegin());
        int a = Days.daysBetween(dt2, dt1).getDays() ;
        double paid = hotelRoomOrder.getPrice()*(a);
        return String.valueOf(paid) ;
    }
    
    public void saveOrder() {
        if (userOrder.getId() == null) {
            userServiceImpl.create(userOrder);
        }
        
        Orders o = new Orders();
        o.setDateOrder(new Date());
        o.setIdUser(userOrder);
        o.setIsPaid(false);
        o.setPrepay(prepay);
        DateTime dt1 = new DateTime(orderRoom.getDateEnd());
        DateTime dt2 = new DateTime(orderRoom.getDateBegin());
        int a = Days.daysBetween(dt2, dt1).getDays() ;
        double paid = hotelRoomOrder.getPrice()*(a);
        o.setTotalCost(paid);
       
        orderServices.create(o);
        
        System.out.println("id akaza:"+o.getId());
        orderRoom.setIdOrder(o);
        orderRoom.setIdRoomNumber(hotelRoomOrder);
        orderRoom.setIsReservation(true);
        orderRoom.setDateReservation(new Date());

        orderRoomServices.create(orderRoom);
        
    }
    
    
    
    
}
